# 0.准备工作
import requests,json
import pandas as pd
xu_key = "9bce68c83a431dbc61cacc81a1de087b"

# 1.地理编码
def geo(key,address,city,batch,sig,output,callback):
    """地理编码"""
    url = "https://restapi.amap.com/v3/geocode/geo?parameters"
    params = {
    "key":key,
    "address":address,
    "city":city,
    "batch":batch,
    "sig":sig,
    "output":output,
    "callback":callback
    }
    r = requests.get(url,params)
    results = r.json()
    return results

# 2.逆地理编码
def regeo(key,location,poitype=None,radius=None,
          extension="base",batch="false",roadlevel=0,
          sig=None,output="json",callback=None,homeorcorp=0):
    """逆地理编码"""
    url = "https://restapi.amap.com/v3/geocode/regeo?parameters"
    params ={
      "key":key,
      "location":location,
      "poitype":poitype,
      "radius":radius,
      "extensions":extension,
      "batch":batch,
      "roadlevel":roadlevel,
      "sig":sig,
      "output":output,
      "callback":callback,
      "homeorcorp":homeorcorp
    }
    r = requests.get(url,params)
    results = r.json()
    return results

# 3.路径规划
## 3.1步行
def walking(key,origin=起始点_中大南方,destination=目的地_龙岗,
           sig=None,output="json",callback=None):
    """步行"""
    walking_url = "https://restapi.amap.com/v3/direction/walking?parameters"
    params = {
        "key":key,
        "origin": origin,
        "destination":destination,
        "sig":sig,
        "output":output,
        "callback":callback
    }
    r=requests.get(walking_url,params=params)
    results=r.json()
    return results

## 3.2 公交
def integrated(key,origin=起始点_江南西,destination=目的地_琶洲,city="广州",
               cityd=None,extensions="all",strategy=0,nightflag=0,date=None,
               time=None,sig=None,output="json",callback=None):
    """公交"""
    integrated_url = "https://restapi.amap.com/v3/direction/transit/integrated?parameters"
    params = {
        "key":key,
        "origin":origin,
        "destination":destination,
        "city":city,
        "cityd":cityd,
        "extensions":extensions, #可选
        "strategy":strategy, #可选
        "nightflag":nightflag,
        "date":date,
        "time":time,
        "sig":sig,
        "output":output,
        "callback":callback
    }
    r = requests.get(integrated_url,params=params)
    results=r.json()
    return results

## 3.3 驾车
def driving(key,origin,destination):
    """驾车"""
    driving_url = "https://restapi.amap.com/v3/direction/driving?parameters"
    params ={
        "key":key,
        "origin":origin,
        "destination":destination
    }
    r = requests.get(driving_url,params)
    results=r.json()
    return results

## 3.4 骑行
def bicycling(key,origin,destination):
    """骑行"""
    bicycling_url = "https://restapi.amap.com/v4/direction/bicycling?parameters"
    params = {
        "key":key,
        "origin":origin,
        "destination":destination
    }
    r = requests.get(bicycling_url,params)
    results=r.json()
    return results

## 3.5 货车
def truck(key,origin,destination,size):
    """货车"""
    truck_url = "https://restapi.amap.com/v4/direction/truck?parameters"
    params ={
        "key":key,
        "origin":origin,
        "destination":destination,
        "size":size
    }
    r = requests.get(truck_url,params)
    results=r.json()
    return results

## 3.6 测量距离
def distance(key,origins,destination,type):
    """测量距离"""
    distance_url='https://restapi.amap.com/v3/distance?parameters'
    key='18ef7dd6aeefc21dade574262c00f213'
    params={
        'key':key,
        'origins':origins,
        'destination':destination,
        'type':type
    }
    response=requests.get(distance_url,params)
    results=response.json()
    # return
    return results

# 4.行政区域查询
def district(key,keywords):
    """行政区域查询"""
    district_url = "https://restapi.amap.com/v3/config/district?parameters"
    params = {
        "key":key,
        "keywords":keywords
        #"subdistrict":0
    }
    r=requests.get(district_url,params=params)
    results=r.json()
    return results

# 5.搜索POI
## 5.1 关键字搜素
def text(key,city,keywords):
    """关键字搜索"""
    text_url = "https://restapi.amap.com/v3/place/text?parameters"
    params = {
        "key":key,
        "city":city,
        "keywords":keywords
    }
    r=requests.get(text_url,params=params)
    results=r.json()
    return results

## 5.2 周边搜素
def around(key,location):
    """周边搜索"""
    around_url = 'https://restapi.amap.com/v3/place/around?parameters'
    params = {
        "key":key,
        "location":location
    }
    r=requests.get(around_url,params)
    results = r.json()
    return results

## 5.3 多边形搜索
def polygon(key,polygon,keywords):
    """多边形搜索"""
    polygon_url = "https://restapi.amap.com/v3/place/polygon?parameters"
    params = {
        "key":key,
        "polygon":polygon,
        "keywords":keywords
    }
    r=requests.get(polygon_url,params)
    results = r.json()
    return results

# 6.ID查询
def detail(key,id):
    """ID查询"""
    detail_url = "https://restapi.amap.com/v3/place/detail?parameters"
    params = {
        "key":key,
        "id":id
    }
    r=requests.get(detail_url,params)
    results = r.json()
    return results

# 7.IP定位
def ip(key):
    """IP定位"""
    ip_url = "https://restapi.amap.com/v3/ip?parameters"
    params={
        "key":key
    }
    r=requests.get(ip_url,params=params)
    results = r.json()
    return results

# 8.静态地图
## 8.1准备
from PIL import Image
from io import BytesIO
## 8.2
def staticmap(ky,location,zoom):
    """静态地图"""
    staticmap_url = "https://restapi.amap.com/v3/staticmap?parameters"
    params ={
        "key":xu_key,
        "location":location,
        "zoom":zoom
    }
    r = requests.get(staticmap_url,params)
    result = r.content
    results = Image.open(BytesIO(r.content))
    return results

# 9.坐标转换
def convert(key,locations):
    """坐标转换"""
    convert_url="https://restapi.amap.com/v3/assistant/coordinate/convert?parameters"
    params = {
        "key":key,
        "locations":locations
    }
    r=requests.get(convert_url,params)
    results = r.json()
    return results

# 10.天气查询
def weatherInfo(key,city):
    """天气查询"""
    weatherInfo_url = "https://restapi.amap.com/v3/weather/weatherInfo?parameters"
    params = {
        "key":key,
        "city":city
    }
    r = requests.get(weatherInfo_url,params)
    results = r.json()
    return results

# 11.输入提示
def inputtips(key,keywords,city):
    """输入提示"""
    inputtips_url = "https://restapi.amap.com/v3/assistant/inputtips?parameters"
    params = {
        "key":key,
        "keywords":keywords,
        "city":city
    }
    r = requests.get(inputtips_url,params)
    results = r.json()
    return results

# 12  交通态势
# 说明：交通态势接口计划将于2020年12月31日下线，目前不再对新用户开放。如需合作请通过工单进行商务咨询。